//
//  CLVisitorLoginView.swift
//  WeiBo
//
//  Created by 程雷 on 2017/1/5.
//  Copyright © 2017年 chenglei. All rights reserved.
//

import UIKit
import SnapKit

class CLVisitorLoginView: UIView {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func updateInfo(title: String, imageName: String, isHome: Bool = false){
    
        self.descLabel.text = title
        self.cycleView.image = UIImage(named: imageName)
        
        if isHome {
            
            startAnimation()
        }else{
        
            self.bigHouse.isHidden = true
            self.backView.isHidden = true
        }
    }
    
    
    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        animation.toValue = M_PI * 2
        animation.duration = 20
        //isRemovedOnCompletion: 当动画效果啊完成之后就从图层移除,设置为false 就不会被移除图层, 默认值值是true
        animation.isRemovedOnCompletion = false
        cycleView.layer.add(animation, forKey: nil)
    }
    
 
    private func setupUI(){
        
        addSubview(cycleView)
        addSubview(bigHouse)
        addSubview(backView)
        addSubview(descLabel)
        addSubview(loginBtn)
        addSubview(rigisterBtn)
        
        
        
        cycleView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        
        bigHouse.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleView)
        }
        
        backView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleView)
        }
        
        descLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(backView.snp.bottom)
            make.centerX.equalTo(self.snp.centerX)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            
            make.left.equalTo(descLabel)
            make.top.equalTo(descLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        
        rigisterBtn.snp.makeConstraints { (make) in
            
            make.right.equalTo(descLabel)
            make.top.equalTo(loginBtn)
            make.width.equalTo(loginBtn)
        }
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    
    private lazy var cycleView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    private lazy var bigHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    private lazy var descLabel: UILabel = {
        
        let descLable = UILabel(title: "关注一些人,关注一些人,回到这里看看有什么惊喜", font: 14, color: UIColor.black)
        descLable.numberOfLines = 0
        descLable.textAlignment = .center
        descLable.preferredMaxLayoutWidth = 220
        
        return descLable
    }()
    
    private lazy var loginBtn: UIButton = {
    
        let btn = UIButton(title: "登录", font: 14, textColor: UIColor.black)
        btn .setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    
    private lazy var rigisterBtn: UIButton = {
    
        let btn = UIButton(title: "注册", font: 14, textColor: UIColor.orange)
        btn .setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
        
    }()
    
}
